<template>
  <el-dialog
    :title="!dataForm.attrGroupId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="90px" v-loading="loading">
    <el-form-item label="组名" prop="attrGroupName">
      <el-input v-model="dataForm.attrGroupName" placeholder="组名"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model.number="dataForm.sort" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="descript">
      <el-input v-model="dataForm.descript" placeholder="描述"></el-input>
    </el-form-item>
    <el-form-item label="组图标" prop="icon">
      <IconSelect :icon-default="dataForm.icon" @icon-click="IconClick"/>
    </el-form-item>
    <el-form-item label="所属分类" prop="catelogIds">
      <el-cascader
        v-model="dataForm.catelogIds"
        :options="categoryList"
        :props="props"
        filterable
        style="width: 100%"></el-cascader>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import IconSelect from '@/components/Common/IconSelect'
  import { list } from "../../../../api/goods/category";
  import { info, add, update } from "../../../../api/goods/attrgroup";

  export default {
    components: {
      IconSelect
    },
    data () {
      return {
        props: {
          value:'catId',
          label:'name'
        },
        visible: false,
        loading: false,
        categoryList: [],
        dataForm: {
          attrGroupId: 0,
          attrGroupName: '',
          sort: '',
          descript: '',
          icon: '',
          catelogIds: [],
          catelogId: 0
        },
        dataRule: {
          attrGroupName: [
            { validator: (rule, value, callback) => {
                if (value === '') {
                  callback(new Error('组名不能为空'))
                } else if (value.length > 10) {
                  callback(new Error('组名不能超过10个字'))
                } else {
                  callback()
                }
              },required: true, trigger: 'blur' }
          ],
          sort: [
            { validator: (rule, value, callback) => {
                if (value === '') {
                  callback(new Error('排序必须填写'))
                } else if ( !Number.isInteger(value) || value < 0 ) {
                  callback(new Error('排序必须是一个大于等于零的整数'))
                } else {
                  callback()
                }
              },required: true, trigger: 'blur' }
          ],
          descript: [
            { validator: (rule, value, callback) => {
                if (value === '') {
                  callback(new Error('描述不能为空'))
                } else if (value.length > 10) {
                  callback(new Error('描述不能超过10个字'))
                } else {
                  callback()
                }
              },required: true, trigger: 'blur' }
          ],
          icon: [
            { required: true, message: '组图标不能为空', trigger: 'blur' }
          ],
          catelogIds: [
            { required: true, message: '所属分类id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      IconClick(icon) {
        this.dataForm.icon = icon
      },
      getList() {
        list()
          .then(request => {
            this.categoryList = request.data
          })
      },
      init (id) {
        this.dataForm.attrGroupId = id || 0
        this.visible = true
        this.loading = true
        this.dataForm.catelogIds = []
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.attrGroupId) {
            info(this.dataForm.attrGroupId)
              .then(({ data }) => {
                this.dataForm.attrGroupName = data.attrGroupName
                this.dataForm.sort = data.sort
                this.dataForm.descript = data.descript
                this.dataForm.icon = data.icon
                this.dataForm.catelogId = data.catelogId
                this.dataForm.catelogIds = data.catelogPath
                this.loading = false
              })
          } else {
            this.loading = false
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            const query = {
              'attrGroupId': this.dataForm.attrGroupId || undefined,
              'attrGroupName': this.dataForm.attrGroupName,
              'sort': this.dataForm.sort,
              'descript': this.dataForm.descript,
              'icon': this.dataForm.icon,
              'catelogId': this.dataForm.catelogIds[this.dataForm.catelogIds.length-1]
            }
            if(!this.dataForm.attrGroupId){
              add(query)
                .then(() => {
                  this.$message({
                    message: '操作成功',
                    type: 'success'
                  })
                  this.visible = false
                  this.$emit('refreshDataList')
                })
            } else {
              update(query)
                .then(() => {
                  this.$message({
                    message: '操作成功',
                    type: 'success'
                  })
                  this.visible = false
                  this.$emit('refreshDataList')
                })
            }
          }
        })
      }
    }
  }
</script>
